//
//  BookDBService.m
//  readNovels
//
//  Created by wulanzhou on 2017/2/27.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

#import "BookDBService.h"
#import "SQLDBManager.h"
#import "BSQLConfig.h"


@implementation BookDBService



- (BOOL)addBookName:(NSString *)name author:(NSString *)author{
    
    Book *entity = [[Book alloc] init];
    entity.name = name;
    entity.author = author;
    entity.status = BookStatusUnKnow;
    entity.ctime = [NSDate date];
    entity.mtime = [NSDate date];

    return [self Insert:entity];
}

#pragma mark - DBInterface Methods
- (BOOL)Insert:(Book *)entity{
    
    NSDateFormatter*dateFormat =[[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    BOOL success = [[SQLDBManager shareInstance] update:insert_book_sql,entity.name,entity.author,[NSNumber numberWithDouble:entity.price],entity.publisher,entity.isbn,[NSNumber numberWithInteger:entity.status],[dateFormat stringFromDate:entity.ctime],[dateFormat stringFromDate:entity.mtime], nil];
    if (success) {
        NSLog(@"新增书籍成功!");
    }else{
        NSLog(@"新增书籍失败!");
    }
    return success;
    
}

- (BOOL)Update:(Book *)entity{

    NSDateFormatter*dateFormat =[[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    BOOL success  = [[SQLDBManager shareInstance] update:update_book_status,[NSNumber numberWithInteger:entity.status],[dateFormat stringFromDate:entity.mtime],[NSNumber numberWithInteger:entity.bId], nil];
    
    if (success) {
        NSLog(@"修改书籍成功!");
    }else{
        NSLog(@"修改书籍失败!");
    }
    return success;
}

- (Book *)Entity:(id)kId{

    NSInteger primaryKey = [kId integerValue];
    NSArray *source = [[SQLDBManager shareInstance] queryModels:[Book class] sql:query_id_book,[NSNumber numberWithInteger:primaryKey], nil];
    
    if (source && [source count] >0) {
        
        return source[0];
    }
    return nil;
}

- (NSArray*)list:(id)map{

    return [[SQLDBManager shareInstance] queryModels:[Book class] sql:query_all_book, nil];
}


@end
